package com.deyuanyun.pic.settings.controller.prvlg;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.common.util.RequestUtil;
import com.deyuanyun.pic.common.util.ResponseUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.service.prvlg.RoleService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/settings/prvlg")
public class RoleController {
	@Resource
	private RoleService roleService;
	//菜单管理页面
	@RequestMapping(value="/getMenus.html",method=RequestMethod.GET)
	public ModelAndView menuPage(ModelAndView mv) {
		mv.setViewName("/settings/prvlg/functionMenu");
		return mv;
	}
	@RequestMapping(value="/getMenus.json",method=RequestMethod.POST)
	@ResponseBody
	public void getMenus(HttpServletRequest request,HttpServletResponse response) {
		HashMap menu= RequestUtil.parseRequestParamToHashMap(request);
		//List page=roleService.getMenus(menu);
		ResponseUtil.printl(response, ResponseUtil.getJson(roleService.getMenus(menu)).toString(), "json");
		return;
	}




//角色管理页面
	@RequestMapping(value="/getRoles.json",method=RequestMethod.POST)
	public void getRoles(HttpServletRequest request,HttpServletResponse response) {
			HashMap param= RequestUtil.parseRequestParamToHashMap(request);
			//Page page=roleService.getRoles(param);
				ResponseUtil.printl(response, ResponseUtil.getJson(roleService.getRoles(param)).toString(), "json");
			}
	
	@RequestMapping(value="/putqx.json",method=RequestMethod.POST)
	@ResponseBody
	public void putqx(String id,String name) {
		Map<String,Object> map = new HashMap<String,Object>();
		map = roleService.putqx(id,name);
		AjaxSupport.sendSuccessText("",map);
	}

	//角色管理页面
		@RequestMapping(value="/getRoles.html",method=RequestMethod.GET)
		public ModelAndView rolepage(ModelAndView mv) {
				mv.setViewName("/settings/prvlg/roleManagement");
				return mv;
			}
		@RequestMapping(value="/saveunauthorized.json",method=RequestMethod.POST)
		public ModelAndView saveunauthorized(HttpServletRequest request) {
			Map param= RequestUtil.parseRequestParamToHashMap(request);
			ModelAndView mv=new ModelAndView();
			try {
				roleService.saveunauthorized(param);
					mv.addObject("status", "0");
			} catch (Exception e) {
				e.printStackTrace();
				mv.addObject("status", "1");
			}
			return mv;
			}
		
	
	
	@RequestMapping(value="/delRole.json",method=RequestMethod.POST)
	public ModelAndView delRole(HttpServletRequest request){
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		ModelAndView mv=new ModelAndView();
		try {
			boolean status=roleService.delRole(param);
			if(status)
			{
				mv.addObject("status", "0");
			}else{
				mv.addObject("status", "1");
				mv.addAllObjects(param);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("status", "1");
		}
		return mv;
	}
	
	@RequestMapping(value="/updateState.json",method=RequestMethod.POST)
	public ModelAndView updateState(HttpServletRequest request){
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		ModelAndView mv = new ModelAndView();
		
			Map map=roleService.updateState(param);

			mv.addObject("status", map.get("m"));

			mv.addObject("statusCode", 200);
		return mv;
	}

	
	@RequestMapping(value="/insertRole.json",method=RequestMethod.POST)
	public ModelAndView insertRole(HttpServletRequest request){
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		ModelAndView mv = new ModelAndView();
		Map status;
		List<String> list=new ArrayList<String>();
		try {
			if(StringUtils.isEmpty(param.get("ROLENAME"))){
				list.add("名称不能为空");
				mv.addObject("errList", list);
			}else{
				status=roleService.insertRole(param);
				if (status.get("err")==null){
					mv.addObject("flag", "yes");
					mv.addObject("status", "0");
				}else {
					list.add( status.get("err").toString());
					mv.addObject("errList",list);
					mv.addObject("status", "1");
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
			mv.addAllObjects(param);
		}
		return mv;
	}
	@RequestMapping(value="/updateRole.json",method=RequestMethod.POST)
	public ModelAndView updateRole(HttpServletRequest request){
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		ModelAndView mv = new ModelAndView();
		try {
			Boolean status=roleService.updateRole(param);
			
			if(status)
			{
				mv.addObject("status", "0");
			}else{
				mv.addObject("status", "1");
				mv.addAllObjects(param);
			}
		} catch (Exception e) {
			e.printStackTrace();
			mv.addObject("status", "1");
		}
		return mv;
	}
	@RequestMapping(value="/getOneRole.json",method=RequestMethod.POST)
	public void getOneRole(HttpServletRequest request,HttpServletResponse response){
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		Page<HashMap<String, Object>> page=roleService.getRoles(param);
			ResponseUtil.printl(response, ResponseUtil.getJson(roleService.getRoles(param)).toString(), "json");
	}
	

	//===============================独立========================================================

	/***
	 * 新增一个角色
	 * @param request
	 * @return
     */
	@RequestMapping(value="role/insertRole1.json")
	@ResponseBody
	public void insertRole1(HttpServletRequest request,
									String roleName,
									String roleCode,
									String remark)throws Exception{
		HashMap map=new HashMap();
		map.put("ROLENAME",roleName);
		map.put("ROLECODE",roleCode);
		map.put("REMARK",remark);
		Map b=roleService.insertRole(map);
		String m="";
		if (b.get("err")!=null){
			m=b.get("err").toString();
			Asserts.assertTrue(false,m);
			return;
		}

		AjaxSupport.sendSuccessText("","添加成功");

	}


	@RequestMapping("updateState1.json")
	@ResponseBody
	public void updateState1(HttpServletRequest request,@RequestParam(value = "dataIds[]",required = false) List<String> dataIds){
		HashMap param= RequestUtil.parseRequestParamToHashMap(request);
		if (ObjectUtil.isNotNullEmpty(dataIds)){
			param.put("idList",dataIds);
		}

		boolean b= ObjectUtil.isEmptyAll(param.get("id")) && ObjectUtil.isEmptyAll(dataIds);

		Asserts.assertTrue(!b,"id不能为空");
		roleService.updateState(param);

		AjaxSupport.sendSuccessText("success","修改成功");
	}
	@RequestMapping(value="role/delRole1.json")
	@ResponseBody
	public void delRole1(HttpServletRequest request,
						 String rid,
						 @RequestParam(value = "dataIds[]",required = false) List<String> dataIds){
		//HashMap param=RequestUtil.parseRequestParamToHashMap(request);
		HashMap map=new HashMap();
		if (ObjectUtil.isNotNullEmpty(dataIds)){
			map.put("idList",dataIds);
		}
		if (org.apache.commons.lang.StringUtils.isNotBlank(rid)){
			map.put("id",rid);
		}
		boolean b= ObjectUtil.isEmptyAll(rid) && ObjectUtil.isEmptyAll(dataIds);

		Asserts.assertTrue(!b,"id不能为空");
		roleService.delRole(map);
		AjaxSupport.sendSuccessText("","删除成功");
		return ;
	}
	@RequestMapping("role/getRolesList.json")
	@ResponseBody
	public void getRolesList(String ROLENAME,String ROLECODE){
		HashMap map=new HashMap();
		if (org.apache.commons.lang.StringUtils.isNotBlank(ROLENAME)){
			map.put("ROLENAME",ROLENAME);
		}
		if (org.apache.commons.lang.StringUtils.isNotBlank(ROLECODE)){
			map.put("ROLCODE",ROLECODE);
		}

		PageJsonBean jsonBean=new PageJsonBean();
		List page=roleService.getRoles(map);
		jsonBean.setList(page);
		AjaxSupport.sendSuccessText("",jsonBean);
		return;
	}

	@RequestMapping("role/getMenuIdByRoleId.json")
	@ResponseBody
	public void getMenuIdByRoleId(String roleId){
		Asserts.assertTrue(org.apache.commons.lang.StringUtils.isNotBlank(roleId),"角色id不能为空");
		AjaxSupport.sendSuccessText("",roleService.queryMenuIdByRoleId(roleId));
		return;
	}

	//保存角色的权限
	@RequestMapping("role/saveRoleMenu.json")
	@ResponseBody
	public void saveRoleMenu(String roleId,@RequestParam(value = "dataIds[]",required = false) List<String> dataIds){
		Asserts.assertTrue(org.apache.commons.lang.StringUtils.isNotBlank(roleId),"roleId不能为空");
		HashMap map=new HashMap();
		map.put("roleId",roleId);
		map.put("menuIdList",dataIds);
		roleService.saveunauthorized(map);
		AjaxSupport.sendSuccessText("","操作成功");
		return;
	}


	@RequestMapping(value="role/updateRole1.json",method=RequestMethod.POST)
	@ResponseBody
	public void updateRole1(HttpServletRequest request,
									String rId,
									String roleName,
									String roleCode,
									String remark){
		Asserts.assertTrue(org.apache.commons.lang.StringUtils.isNotBlank(rId),"id不能为空");
		HashMap map=new HashMap();
		map.put("ID_",rId);
		map.put("ROLENAME",roleName);
		map.put("ROLECODE",roleCode);
		map.put("REMARK",remark);
		Boolean status=roleService.updateRole(map);
		Asserts.assertTrue(status,"更新失败");
		AjaxSupport.sendSuccessText("","更新成功");
return ;

	}
}
